<template>
  <div class="org-container">
    <el-card>
      <search-tool @onSearch="handleSearch"></search-tool>
      <div class="add-btn" @click="handleAdd">
        <!-- <svg-icon class="add-btnicon"
                  icon="planlist-add"></svg-icon> -->
        <span class="addFont">新增</span>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        style="width: 100%"
        height="528px"
      >
        <el-table-column label="序号" type="index" width="80" />
        <el-table-column prop="name" label="部门名称"> </el-table-column>
        <el-table-column prop="description" label="描述"> </el-table-column>
        <el-table-column prop="createTime" label="创建时间"> </el-table-column>
        <el-table-column prop="updateTime" label="更新时间"> </el-table-column>
        <el-table-column label="操作" fixed="right" width="260">
          <template #default="{ row }">
            <div class="action-row">
              <span class="action-btn" @click="handleEdit(row)">修改</span>
              <div class="has-more"></div>
              <span class="action-btn" @click="handleDelete(row.id)">删除</span>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[5, 10, 20]"
        :page-size="size"
        layout="prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <add-dialog
      :title="dialogTitle"
      :rowData="curRowData"
      v-model:visible="visibleAddDialog"
      @onReload="reloadPage"
    ></add-dialog>
    <edit-dialog
      :title="dialogTitle"
      :rowData="curRowData"
      v-model:visible="visibleDialog"
      @onReload="reloadPage"
    ></edit-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import SearchTool from './components/SearchTool/index.vue'
import EditDialog from './components/EditDialog/index.vue'
import AddDialog from './components/AddDialog/index .vue'
import { ElMessageBox } from 'element-plus'
import { dept, delDept } from '@/api/dept'

//数据相关
const tableData = ref([])
const total = ref(0)
const page = ref(1)
const size = ref(10)
const params = ref({})
const loading = ref(false)
const visibleDialog = ref(false)
const visibleAddDialog = ref(false)
const curRowData = ref({})
const dialogTitle = ref('')

//获取数据的方法
const getListData = async (params) => {
  loading.value = true
  const result = await dept(params, page.value, size.value)
  loading.value = false
  tableData.value = result.rows
  total.value = result.total
}

const reloadPage = () => {
  getListData(params.value)
  if (visibleDialog.value || visibleAddDialog.value) {
    visibleDialog.value = false
    visibleAddDialog.value = false
  }
}

reloadPage()
//查询条件相关
const handleSearch = (paramsData) => {
  params.value = paramsData
  getListData(paramsData)
}

// 分页相关
/**
 * size 改变触发
 */
const handleSizeChange = (currentSize) => {
  size.value = currentSize
  getListData(params.value)
}

/**
 * 页码改变触发
 */
const handleCurrentChange = (currentPage) => {
  page.value = currentPage
  getListData(params.value)
}

const handleAdd = () => {
  dialogTitle.value = '新增机构'
  curRowData.value = {}
  visibleAddDialog.value = true
}
const handleEdit = (row) => {
  dialogTitle.value = '编辑机构'
  curRowData.value = row
  visibleDialog.value = true
}

const handleDelete = (
  id,
  done = async () => {
    // 删除
    await delDept(id)
    reloadPage()
  }
) => {
  ElMessageBox.confirm('确定删除该机构吗?', '删除机构', {
    confirmButtonText: '确定',
    cancelButtonText: '取消'
  })
    .then(() => {
      done()
    })
    .catch(() => {
      // catch error
    })
}
</script>

<style lang="scss" scoped>
.org-container {
  width: 100%;
  .add-btn {
    width: 74px;
    height: 36px;
    background: #1e6ae7;
    opacity: 1;
    border: 1px solid #8bb7ff;

    position: relative;
    margin-bottom: 24px;
    .addFont {
      position: absolute;
      font-size: 14px;
      font-family: Microsoft YaHei-Regular, Microsoft YaHei;
      font-weight: 400;
      color: #ffffff;
      top: 7px;
      left: 20px;
    }
  }
  .btns {
    position: relative;
    width: 100%;
    display: flex;
    height: 40px;
    align-items: center;
    margin-bottom: 20px;
  }
  .action-row {
    display: flex;
    .action-btn {
      position: relative;
      font-size: 14px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      line-height: 19px;
      color: #3475ff;
      cursor: pointer;
    }
    .has-more {
      width: 2px;
      height: 10px;
      background: #0052d9;
      margin-right: 8px;
      margin-left: 8px;
      margin-top: 5px;
    }
  }
  .pagination {
    width: 100%;
    height: 32px;
    margin-top: 20px;
    display: flex;
    align-items: center;
    justify-content: end;
  }
  :deep(.el-dialog__header) {
    background-color: rgba(24, 144, 255, 1);
  }
  :deep(.el-dialog__title) {
    font-family: '微软雅黑 Bold', '微软雅黑 Regular', '微软雅黑';
    font-weight: 700;
    font-style: normal;
    font-size: 16px;
    color: #ffffff;
    text-align: left;
  }
  :deep(.el-pager li.active) {
    width: 32px;
    height: 32px;
    background: rgba(255, 255, 255, 0.39);
    border: 1px solid #2a5eff;
    border-radius: 3px;
  }
  :deep(.el-pager li.active.number) {
    font-size: 14px;
    font-family: Microsoft YaHei;
    color: rgba(0, 0, 0, 0.6);
  }
  :deep(.el-pager li:hover) {
    color: rgba(0, 0, 0, 0.6);
    border: 1px solid #2a5eff;
  }
}
</style>
